home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / sphigs / sph_srgp.lha / srgp / src / srgp_pattern.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-09  |  9.0 KB  |  368 lines

  1. #include "HEADERS.h"
  2. #include "srgplocal.h"
  3. #include "default_patterns.h"
  4.  
  5.  
  6. #define BUFFERSIZE   150
  7.  
  8. static char   inputline [BUFFERSIZE+1];
  9.  
  10. #ifdef X11
  11.    static XImage *ximage_pix, *ximage_bit;
  12.    static GC     putimagegc_pix, putimagegc_bit;
  13. #endif
  14.  
  15.  
  16.  
  17. /** INITIALIZATION OF THE PATTERN TABLE
  18. Bitmap pattern table is initialized; or at least a large portion of
  19. it is initialized.
  20.  
  21. The pixmap pattern table's 0th element is initialized to all 0.
  22. No other pixmap patterns are initialized.
  23.  
  24. WARNING: CURRENTLY THE MAC VERSION INIT'S NO PIXMAPS.
  25.  
  26. This routine also initializes some static variables that are 
  27. necessary for making runtime changes to the pixmap table.
  28. **/
  29.  
  30. void
  31. SRGP__initDefaultPatterns()
  32. {
  33.    register i;
  34.  
  35.    /******** bitmap table */
  36.    for (i=0; i <= 104; i++) {
  37. #ifdef X11
  38.       srgp__bitmapPatternTable[i] = 
  39.      XCreateBitmapFromData
  40.         (srgpx__display, srgp__canvasTable[0].drawable.win, 
  41.          default_bitpat[i], 8, 8);
  42. #endif
  43. #ifdef THINK_C
  44.       memcpy (srgp__bitmapPatternTable[i], default_bitpat[i], (size_t)8);
  45. #endif
  46.    }
  47.    
  48. #ifdef X11
  49.    for ( ; i <= MAX_PATTERN_INDEX; i++)
  50.       srgp__bitmapPatternTable[i] = NULL;
  51. #endif
  52.  
  53.  
  54.    /******** pixmap table */
  55. #ifdef X11
  56.    srgp__pixmapPatternTable[0] = 
  57.       XCreatePixmapFromBitmapData
  58.      (srgpx__display, srgp__canvasTable[0].drawable.win, 
  59.       default_bitpat[0], 8, 8,
  60.       COLORINDEX(SRGP_BLACK), COLORINDEX(SRGP_WHITE), 
  61.       srgp__available_depth);
  62. #endif
  63. #ifdef THINK_C
  64.    srgp__pixmapPatternTable[0] = NULL;
  65. #endif
  66.  
  67.    for (i=1; i <= MAX_PATTERN_INDEX; i++)
  68.       srgp__pixmapPatternTable[i] = NULL;
  69.  
  70.  
  71. #ifdef X11
  72.    /******** imaging variables */
  73.    putimagegc_pix =    
  74.       XCreateGC (srgpx__display, srgp__canvasTable[0].drawable.win,
  75.          0L, NULL);
  76.    putimagegc_bit =    
  77.       XCreateGC (srgpx__display, srgp__bitmapPatternTable[0],
  78.          0L, NULL);
  79.    ximage_pix = XGetImage (srgpx__display,
  80.                srgp__pixmapPatternTable[0],
  81.                0,0, 8,8,    
  82.                0xffff,
  83.                XYPixmap);
  84.    ximage_bit = XGetImage (srgpx__display,
  85.                srgp__bitmapPatternTable[0],
  86.                0,0, 8,8,    
  87.                1,
  88.                XYPixmap);
  89. #endif
  90. }
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97. /** LOAD A SINGLE BITMAP PATTERN
  98. Application must send data in the form of 8 characters, the
  99. same format created by the X bitmap editor.
  100. **/
  101.  
  102. void SRGP_loadBitmapPattern (int pattern_id, char *data)
  103. {
  104.    register i,j;
  105.  
  106.  
  107.    DEBUG_AIDS{
  108.       SRGP_trace (SRGP_logStream, "SRGP_loadBitmapPattern %d %x\n", 
  109.           pattern_id, data);
  110.       srgp_check_system_state();
  111.       LeaveIfNonFatalErr();
  112.    }
  113.  
  114. #ifdef X11
  115.    /* CREATE THE BITMAP (if not already) */
  116.    if (NULL == srgp__bitmapPatternTable[pattern_id]) 
  117.       srgp__bitmapPatternTable[pattern_id] = 
  118.      XCreatePixmap
  119.         (srgpx__display, srgp__canvasTable[0].drawable.win, 8, 8,
  120.          1);
  121.  
  122.    if (NULL == srgp__bitmapPatternTable[pattern_id]) {
  123.       SRGP__error (ERR_PATTERN_ALLOCATION);
  124.       return;
  125.    }
  126.  
  127.    /* PAINT THE PIXELS */
  128.    for (i=0; i<8; i++)
  129.       for (j=7; j>=0; j--)
  130.      XPutPixel (ximage_bit, i,j, (data[i]&(1<<j))?XBLACK:XWHITE);
  131.  
  132.  
  133.    /* PUT THE XIMAGE INTO THE BITMAP */
  134.    XPutImage (srgpx__display,
  135.           srgp__bitmapPatternTable[pattern_id],
  136.           putimagegc_bit,
  137.           ximage_bit,
  138.           0,0, 0,0, 8,8);
  139. #endif
  140.  
  141. #ifdef THINK_C
  142.    memcpy (srgp__bitmapPatternTable[pattern_id], data, 8);
  143. #endif
  144. }
  145.  
  146.  
  147.  
  148.  
  149. #ifdef X11
  150.  
  151. /** LOAD A SINGLE PIXMAP PATTERN
  152. Application must send data in the form of 8x8 array of integers.
  153. The first integer is the color value for the top-left pixel.
  154. The second integer is for the 2nd pixel on the top row.
  155.    etc.
  156. **/
  157.  
  158. void SRGP_loadPixmapPattern (int pattern_id, int *data)
  159. {
  160.    register i,j;
  161.  
  162.  
  163.    DEBUG_AIDS{
  164.       SRGP_trace (SRGP_logStream, "SRGP_loadPixmapPattern %d %x\n", 
  165.           pattern_id, data);
  166.       srgp_check_system_state();
  167.       LeaveIfNonFatalErr();
  168.    }
  169.  
  170.    /* CREATE THE PIXMAP (if not already) */
  171.    if (NULL == srgp__pixmapPatternTable[pattern_id]) 
  172.       srgp__pixmapPatternTable[pattern_id] = 
  173.      XCreatePixmap
  174.         (srgpx__display, srgp__canvasTable[0].drawable.win, 8, 8,
  175.          srgp__available_depth);
  176.    if (NULL == srgp__pixmapPatternTable[pattern_id]) {
  177.       SRGP__error (ERR_PATTERN_ALLOCATION);
  178.       return;
  179.    }
  180.  
  181.    /* PAINT THE PIXELS */
  182.    for (i=0; i<8; i++)
  183.       for (j=7; j>=0; j--)
  184.       XPutPixel (ximage_pix, i,j, XCOLOR(COLORINDEX(data[(i<<3)+j])));
  185.  
  186.  
  187.    /* PUT THE XIMAGE INTO THE PIXMAP */
  188.    XPutImage (srgpx__display,
  189.           srgp__pixmapPatternTable[pattern_id],
  190.           putimagegc_pix,
  191.           ximage_pix,
  192.           0,0, 0,0, 8,8);
  193. }
  194.  
  195. #endif
  196.  
  197.  
  198.  
  199. /** LOAD BITMAP PATTERNS 
  200. This command reads from an input stream (already
  201. opened by the caller) that should contain one or more bitmap pattern specs.
  202. It reads until EOF is seen, or until any parsing error occurs.
  203.  
  204. Each spec must occupy exactly two lines.  The first line must look like this:
  205.  
  206. static char bitpat_X[] = {
  207.  
  208. where X is a non-negative integer.
  209.  
  210. The second line must specify 8 hexademical numbers in this format:
  211.  
  212.    0x??, 0x??, 0x??, 0x??, 0x??, 0x??, 0x??, 0x??};
  213.  
  214. Each 2-hexdigit number is 8 bits and thus describes one "scan line" 
  215. of the pattern.
  216.  
  217. This format is compatible with the output of
  218. the X bitmap editor (called, appropriately, "bitmap") AS LONG AS
  219. that editor is invoked in this manner:
  220.    
  221.     bitmap bitpat_X 8x8
  222.  
  223. The output files from several bitmap editing sessions can be concatenated
  224. to form a single file defining many bitmap patterns, to be loaded
  225. in a single call to SRGP_loadBitmapPatterns.
  226.  
  227. As a convenience, any lines beginning with '#' are ignored, but these
  228. comment lines must not interrupt a single two-line spec sequence.
  229.  
  230. The closing of the input stream must be performed by the caller.
  231.  
  232. RETURNS 0 if no problem opening the file and parsing the input.
  233. RETURNS 1 if any problems at all occurred.
  234. **/
  235.  
  236. int
  237. SRGP_loadBitmapPatternsFromFile (stream)
  238. FILE *stream;
  239.    int intbit[8];
  240.    char charbit[8];
  241.    int i, pattern_id, retval;
  242.  
  243.  
  244.    DEBUG_AIDS{
  245.       srgp_check_system_state();
  246.       SRGP_trace (SRGP_logStream, "SRGP_loadBitmapPatternsFromFile %x\n", 
  247.           stream);
  248.       LeaveIfNonFatalErr();
  249.    }
  250.  
  251.    PUSH_TRACE;
  252.    while (fgets(inputline, BUFFERSIZE, stream)) {   /* WHILE NOT EOF */
  253.  
  254.       /* IGNORE COMMENT LINES. */
  255.       if (inputline[0] == '#')
  256.      continue;
  257.  
  258.       /* PARSE TO FIND PATTERN INDEX. */
  259.       if (sscanf (inputline, "static char bitpat_%d", &pattern_id) != 1)
  260.      return 1;  /* ERROR! spec start doesn't match required format */
  261.  
  262.       /* GET NEXT LINE: IT CONTAINS THE PATTERN BITS. */
  263.       if ( ! fgets(inputline, BUFFERSIZE, stream))
  264.      return 1;  /* ERROR! premature eof */
  265.  
  266.       /* PARSE THE PATTERN BITS LINE */
  267.       retval = sscanf (inputline,
  268.                " 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x",
  269.                &intbit[0], &intbit[1], &intbit[2], &intbit[3], 
  270.                &intbit[4], &intbit[5], &intbit[6], &intbit[7]);
  271.       if (retval != 8)
  272.      return 1;  /* ERROR! spec 2nd line doesn't match required format */
  273.  
  274.       /* TRANSFORM THE INT-ARRAY SPEC INTO CHAR-ARRAY SPEC. */
  275.       for (i=0; i<8; i++)
  276.      charbit[i]=intbit[i];
  277.  
  278.       PUSH_TRACE;
  279.       SRGP_loadBitmapPattern (pattern_id, charbit);
  280.       POP_TRACE;
  281.    }
  282.  
  283.    return 0;
  284. }
  285.  
  286.  
  287.  
  288.  
  289.  
  290. #ifdef X11
  291.  
  292. /** LOAD PIXMAP PATTERNS FROM FILE
  293. This command reads from an input stream (already
  294. opened by the caller) that should contain one or more pixmap pattern specs.
  295. It reads until EOF is seen, or until any parsing error occurs.
  296.  
  297. Each spec must occupy exactly nine lines.  The first line must look like this:
  298.  
  299. static int pixpat_X[] = {
  300.  
  301. where X is a non-negative integer.
  302.  
  303. The next eight lines each describes one "scan line" of the pixmap pattern,
  304. looking like this:
  305.  
  306.    ?, ?, ?, ?, ?, ?, ?, ?
  307.  
  308. Each question mark represents a non-negative decimal color-value number.
  309.  
  310. As a convenience, any lines beginning with '#' are ignored, but these
  311. comment lines must not interrupt a single nine-line spec sequence.
  312.  
  313. The closing of the input stream must be performed by the caller.
  314.  
  315. RETURNS 0 if no problem opening the file and parsing the input.
  316. RETURNS 1 if any problems at all occurred.
  317. **/
  318. int
  319. SRGP_loadPixmapPatternsFromFile (stream)
  320. FILE *stream;
  321. {
  322.    register i;
  323.    int pattern_id, retval;
  324.    int data[8][8];
  325.       
  326.  
  327.    DEBUG_AIDS{
  328.       srgp_check_system_state();
  329.       SRGP_trace (SRGP_logStream, "SRGP_loadBitmapPatternsFromFile %x\n", 
  330.           stream);
  331.       LeaveIfNonFatalErr();
  332.    }
  333.  
  334.    while (fgets(inputline, BUFFERSIZE, stream)) {   /* WHILE NOT EOF */
  335.  
  336.       /* IGNORE COMMENT LINES. */
  337.       if (inputline[0] == '#')
  338.      continue;
  339.  
  340.       /* PARSE TO FIND PATTERN INDEX. */
  341.       if (sscanf (inputline, "static int pixpat_%d", &pattern_id) != 1)
  342.      return 1;  /* ERROR! spec start doesn't match required format */
  343.  
  344.       /* GET AND PROCESS THE NEXT EIGHT LINES */
  345.       for (i=0; i<8; i++) {
  346.  
  347.      if ( ! fgets(inputline, BUFFERSIZE, stream))
  348.         return 1;  /* ERROR! premature eof */
  349.  
  350.      retval = sscanf (inputline,
  351.               "%d, %d, %d, %d, %d, %d, %d, %d",
  352.               &data[i][0], &data[i][1], &data[i][2], &data[i][3], 
  353.               &data[i][4], &data[i][5], &data[i][6], &data[i][7]);
  354.      if (retval != 8)
  355.         return 1;  /* ERROR! spec line doesn't match required format */
  356.       }
  357.  
  358.       PUSH_TRACE;
  359.    SRGP_loadPixmapPattern (pattern_id, &(data[0][0]));
  360.    POP_TRACE;
  361.    }
  362.  
  363.    return 0;
  364. }
  365.  
  366. #endif
  367.